#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2024-05-17 15:21
# @Author  : nedncs
# @File    : run.py
# @Description : src/python_ldap_admin/config
import logging

import utils

defConfig = """# config.toml
[sys]
debug_mode = false
log_level = "info"
"""
from core.common.logger import logger
import tomlkit
import Env
from pathlib import Path


class Config(object):
    """程序配置类"""

    def __init__(self, filename=None):
        if filename == None:
            filename = Env.CONFIG_FILE
        if filename.exists():
            try:
                with open(filename, mode='rt') as fp:
                    data = tomlkit.load(fp)
                pass
            except Exception as e:
                logger.debug(e)

        else:
            logger.error('文件不存在：{}'.format(filename))
            logger.info('正在创建文件：{}'.format(filename))
            data = tomlkit.loads(defConfig)
            try:
                with open(filename, mode='w') as fp:
                    tomlkit.dump(data, fp)
                pass
            except Exception as e:
                logger.debug(e)
        # 加载对象
        self.log = logger
        self.toml_data = data
        self.reLoadConfig()

    def reLoadConfig(self):
        # 读取配置
        self.debug_mode = self.toml_data.get('sys').get('debug_mode')
        self.log_level = self.toml_data.get('sys').get('log_level')
        self.rsa_public_file = self.toml_data.get('sys').get('rsa_public_file')
        self.rsa_private_file = self.toml_data.get('sys').get('rsa_private_file')


config = Config()
"""配置文件实例"""
